package com.lyx.springmvc.Controller;

import com.lyx.springmvc.pojo.User;
import com.lyx.springmvc.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
@Controller
public class LoginController {
    @Resource
    private UserService userService;
    @PostMapping("/login")
    public String login(String username,String password){
        User user = userService.getUser(username, password);
        if(user==null){
            return "/index.html";
        }
        return "/welcome.html";
    }
    @GetMapping("/register")
    public String returnRegister(){
        return "/register.html";
    }
    @PostMapping("/register")
    public String register(String username,String password,String password2,String sex,String telphone,String email){
        if(!password.equals(password2)){
            return "redirect:/register.html";
        }
        User user=new User(null,username,password,sex,telphone,email);
        boolean flag = userService.getUserForm(user);
        if(flag){
            return "redirect:/register.html";
        }
        boolean b = userService.insert(user);
        if(b){
            return "/welcome.html";
        }
        return "redirect:/register.html";
    }

}
